#include <jni.h>

//
// Created by pengmutian on 2025/9/30.
//
// 自动释放JNI字符串
class JNIStringAutoRelease {
public:
    JNIStringAutoRelease(JNIEnv* env, jstring jstr) : env_(env), jstr_(jstr),
                                                      cstr_(nullptr) {
        if (jstr_ != nullptr) {
            cstr_ = env_->GetStringUTFChars(jstr_, nullptr);
        }
    }

    ~JNIStringAutoRelease() {
        if (jstr_ != nullptr && cstr_ != nullptr) {
            env_->ReleaseStringUTFChars(jstr_, cstr_);
        }
    }

    const char* get() const { return cstr_; }
    operator const char*() const { return cstr_; }

private:
    JNIEnv* env_;
    jstring jstr_;
    const char* cstr_;
};